const mysql = require("mysql")
const pool = mysql.createPool({
    host:"localhost",  
    port:3306,  
    user:"root", // 测试用户
    password:"cui123", // 测试密码
    database:"market" // 数据库名
})
function query(sql){
    return new Promise((resolve,reject) => {
        pool.getConnection(function(err,connection){
            if(err){
                reject(err)
                resolve({status:500,message:"数据库连接出错"})
            }else{
                connection.query(sql,function(err,rows){
                    if(err){
                        reject(err)
                        resolve({
                            status:400,
                            message:"error"
                        })
                    }else{
                        connection.release()
                        resolve({
                            status:200,
                            message:"success",
                            rows
                        }) 
                    }
                })
            }
            
        })
    })
}
exports.query = query
